package com.colorlight.cloud.complete.entity.po;

import com.colorlight.cloud.gateway.api.message.constraint.basics.object.Trans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import com.google.protobuf.ByteString;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * @author Demon
 * @description 加密的细节
 * @date 6/5/2024  上午8:52
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuthorizationDetail implements Trans<AuthorizationDetail, CompleteEntity.AuthorizationDetail> {
    /**
     * 发送卡序号
     * 加密密码
     * 授权截止时间 hex 通过 aes加密后的string
     * 厂商、密码等信息 hex 通过aes 加密后的string
     */
    private Integer prcIndex;
    private String password;
    private byte[] file1;
    private byte[] file2;

    @Override
    public CompleteEntity.AuthorizationDetail toProto() {
        CompleteEntity.AuthorizationDetail.Builder builder = CompleteEntity.AuthorizationDetail.newBuilder();
        if (Objects.nonNull(file1)) {
            builder.setFile1(ByteString.copyFrom(file1));
        }
        if (Objects.nonNull(file2)) {
            builder.setFile2(ByteString.copyFrom(file2));
        }
        if (Objects.nonNull(this.getPassword())) {
            builder.setPassword(this.getPassword());
        }
        if (Objects.nonNull(this.getPrcIndex())) {
            builder.setPrcIndex(this.getPrcIndex());
        }
        return builder.build();
    }

    @Override
    public AuthorizationDetail formProto(CompleteEntity.AuthorizationDetail authorizationDetail) {
        this.setFile1(Objects.nonNull(authorizationDetail.getFile1()) ? authorizationDetail.getFile1().toByteArray() : null);
        this.setFile2(Objects.nonNull(authorizationDetail.getFile2()) ? authorizationDetail.getFile2().toByteArray() : null);
        this.setPassword(authorizationDetail.getPassword());
        this.setPrcIndex(authorizationDetail.getPrcIndex());
        return this;
    }
}
